<template>
  <view class="container">
    <view class="header">
      <view class="avatar-container">
        <image class="avatar" :src="userInfo.avatarUrl || '/static/default-avatar.png'" />
      </view>
      <text class="nickname">{{ userInfo.nickName || '未登录' }}</text>
    </view>

    <view class="info-section">
      <view class="info-title">
        <text>基本信息</text>
      </view>
      <view class="info-item" v-if="userInfo">
        <text class="label">性别</text>
        <text class="value">{{ formatGender(userInfo.gender) }}</text>
      </view>
      <view class="info-item" v-if="userInfo">
        <text class="label">地区</text>
        <text class="value">{{ userInfo.province }} {{ userInfo.city }}</text>
      </view>
    </view>

    <view class="action-section">
      <button class="action-button" @click="navigateToFeedback">
        <uni-icons type="help" size="20"></uni-icons>
        <text>意见反馈</text>
      </button>
      <button class="action-button" @click="clearCache">
        <uni-icons type="trash" size="20"></uni-icons>
        <text>清除缓存</text>
      </button>
      <button class="action-button" @click="about">
        <uni-icons type="info" size="20"></uni-icons>
        <text>关于我们</text>
      </button>
    </view>

    <button class="logout-button" @click="logout" v-if="userInfo">退出登录</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      userInfo: null
    };
  },
  onLoad() {
    this.loadUserInfo();
  },
  methods: {
    // 加载用户信息
    loadUserInfo() {
      try {
        const userInfoStr = uni.getStorageSync('userInfo');
        if (userInfoStr) {
          this.userInfo = JSON.parse(userInfoStr);
        }
      } catch (e) {
        console.error('获取用户信息失败', e);
      }
    },

    // 格式化性别
    formatGender(gender) {
      switch (gender) {
        case 1: return '男';
        case 2: return '女';
        default: return '未设置';
      }
    },

    // 意见反馈
    navigateToFeedback() {
      uni.showToast({
        title: '功能开发中',
        icon: 'none'
      });
    },

    // 清除缓存
    clearCache() {
      uni.showModal({
        title: '提示',
        content: '确定要清除应用缓存吗？',
        success: (res) => {
          if (res.confirm) {
            // 保留登录信息，清除其他缓存
            const token = uni.getStorageSync('token');
            const userInfo = uni.getStorageSync('userInfo');

            uni.clearStorageSync();

            // 恢复登录信息
            if (token) uni.setStorageSync('token', token);
            if (userInfo) uni.setStorageSync('userInfo', userInfo);

            uni.showToast({
              title: '缓存已清除',
              icon: 'success'
            });
          }
        }
      });
    },

    // 关于我们
    about() {
      uni.showModal({
        title: '关于文旅莒县',
        content: '文旅莒县是一款提供莒县文化旅游服务的应用，版本号：1.0.0',
        showCancel: false
      });
    },

    // 退出登录
    logout() {
      uni.showModal({
        title: '提示',
        content: '确定要退出登录吗？',
        success: (res) => {
          if (res.confirm) {
            uni.removeStorageSync('token');
            uni.removeStorageSync('userInfo');
            this.userInfo = null;

            uni.showToast({
              title: '已退出登录',
              icon: 'success'
            });

            // 返回上一页
            setTimeout(() => {
              uni.navigateBack();
            }, 1500);
          }
        }
      });
    }
  }
};
</script>

<style>
.container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.header {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 30px 0;
  background-color: #ffffff;
  border-radius: 10px;
  margin-bottom: 20px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

.avatar-container {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  overflow: hidden;
  margin-bottom: 10px;
  border: 2px solid #f0f0f0;
}

.avatar {
  width: 100%;
  height: 100%;
}

.nickname {
  font-size: 18px;
  font-weight: bold;
}

.info-section,
.action-section {
  background-color: #ffffff;
  border-radius: 10px;
  padding: 15px;
  margin-bottom: 20px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

.info-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 10px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid #f9f9f9;
}

.info-item:last-child {
  border-bottom: none;
}

.label {
  color: #666;
}

.value {
  color: #333;
}

.action-button {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding: 15px 0;
  border-bottom: 1px solid #f9f9f9;
}

.action-button:last-child {
  border-bottom: none;
}

.action-button text {
  margin-left: 10px;
  color: #333;
}

.logout-button {
  width: 100%;
  background-color: #ff4d4f;
  color: white;
  padding: 12px 0;
  border-radius: 5px;
  font-size: 16px;
  margin-top: 30px;
}
</style>
